// axios 配置
import axios from "axios";
// 创建axios实例
const request = axios.create({
  //baseURL: "",
  timeout: 3000, // 请求的超时时间
  // headers: {
  // "Content-Type": "application/x-www-form-urlencoded"
  // "Authorization": "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2Mzk3MDczOTEsInVzZXJfbmFtZSI6ImFkbWluIiwiYXV0aG9yaXRpZXMiOlsidXNlcjp2aWV3IiwidXNlcjp1cGRhdGUiLCJ1c2VyOmFkZCIsInVzZXI6ZGVsZXRlIl0sImp0aSI6IjI3MDg5NGY3LWZlNDgtNDUzYy1hYTM3LWQ0NWEzZjE1NzljNCIsImNsaWVudF9pZCI6Im9jZWFuIiwic2NvcGUiOlsiYWxsIl19.W0EIdvD3sSHSo8SVIEkl8clA9-IrXaNn4vlL0RuhZYY",
  // },
  // withCredentials: true //允许携带cookie
});
// 发送请求前处理request的数据
axios.defaults.transformRequest = [
  function (data) {
    let newData = "";
    for (let k in data) {
      newData +=
        encodeURIComponent(k) + "=" + encodeURIComponent(data[k]) + "&";
    }
    return newData;
  },
];
// request拦截器
request.interceptors.request.use(
  (config) => {
    // 发送请求之前，要做的业务

    return config;
  },
  (error) => {
    // 错误处理代码
    return Promise.reject(error);
  }
);
// response拦截器
request.interceptors.response.use(
  (response) => {
    // 数据响应之后，要做的业务
    return response;
  },
  (error) => {
    return Promise.reject(error);
  }
);
export default request;
